package useranswer

import (
	"go-interview-forum/model/interview"
	"go-interview-forum/model/user"
)

// 刷题添加记录
func AnswerCreateRecord(userid, InterviewsId int64, pid int64) error {
	answer := user.UserAnswer{
		UserID:       userid,
		InterviewsId: InterviewsId,
		PId:          pid,
	}
	err := answer.UserAnswerCreate()
	if err != nil {
		return err
	}
	return nil
}

//用户刷题记录列表

type Datas struct {
	user.UserAnswer
	Interview interview.Interview `json:"interview"`
}

func AnswerRecordList(userid int64, pid int64) (*[]Datas, error) {

	var answer user.UserAnswer
	list, err := answer.UserAnswerList(userid, pid)
	if err != nil {
		return nil, err
	}

	infoMap := make(map[int64]user.UserAnswer)
	for _, v := range list {
		infoMap[v.InterviewsId] = v
	}

	interview, err := interview.ListInterview(0, nil)
	if err != nil {
		panic(err)
	}

	var data []Datas

	for _, v := range interview {
		if _, ok := infoMap[int64(v.ID)]; !ok {
			continue
		}
		if infoMap[int64(v.ID)].InterviewsId == int64(v.ID) {
			data = append(data, Datas{
				UserAnswer: infoMap[int64(v.ID)],
				Interview:  *v,
			})
		}
	}

	return &data, err

}

// 用户刷题默认记录列表
type Data struct {
	user.UserAnswer
	Interview interview.Interview `json:"interview"`
}

func AnswerRecordListAll(userid int64, pid int64, page int, pageSize int) (*[]Data, error) {
	var answer user.UserAnswer

	list, err := answer.UserAnswerListAll(userid, pid, page, pageSize)
	if err != nil {
		return nil, err
	}

	infoMap := make(map[int64]user.UserAnswer)
	for _, v := range list {
		infoMap[v.InterviewsId] = v
	}

	interview, err := interview.ListInterview(0, nil)
	if err != nil {
		panic(err)
	}

	var data []Data

	for _, v := range interview {
		if _, ok := infoMap[int64(v.ID)]; !ok {
			continue
		}
		if infoMap[int64(v.ID)].InterviewsId == int64(v.ID) {
			data = append(data, Data{
				UserAnswer: infoMap[int64(v.ID)],
				Interview:  *v,
			})
		}
	}

	return &data, err
}

//刷题记录删除

func UserAnswerDelete(id []int64, userId, pid int64) error {
	var answer user.UserAnswer
	err := answer.UserAnswerDelete(id, userId, pid)
	if err != nil {
		return err
	}
	return nil

}
